<template>
  <div class="app-container">
    <div class="Header">
      <edit-form ref="filterContainer" v-show="showSearch" :editFormMap="filterMap" :editFormData="queryParams">
        <template slot="buttonGroup">
          <el-col :span="4">
            <el-form-item class="buttonGroup">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </template>
      </edit-form>
      <handle-button-group
        :showSearch.sync="showSearch"
        :single="single"
        :multiple="multiple"
        :authModule="{ module: 'meet', name: 'appointment' }"
        :btnAuth="{ add: false, edit: false, delete: true, export: true }"
        @handleAdd="handleAdd"
        @handleUpdate="handleUpdate"
        @handleDelete="handleDelete"
        @handleExport="handleExport"
        @getList="getList"
      />
    </div>
    <table-list
      ref="tableListContainer"
      :tableListMap="tableMap"
      :request="{
        apiKey: 'appointmentList',
        params: queryParams,
        method: 'get',
      }"
      :tableDataList="tableDataList"
      @selection-change="handleSelectionChange"
      @changeTableListAction="
        (TableList) => {
          userList = TableList;
        }
      "
    >
      <template slot="hasSelect">
        <el-table-column type="selection" width="55" :align="'center'" />
      </template>
      <template slot="hasHandle">
        <el-table-column label="操作" :align="'center'" width="170">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['meet:appointment:view']" @click="handleView(scope.row)">查看</el-button>
            <el-button
              v-show="validAccessAction(scope, 'confirm')"
              size="mini"
              type="text"
              icon="el-icon-circle-check"
              v-hasPermi="['meet:appointment:confirm']"
              @click="handleConfirm(scope.row)"
            >
              确认
            </el-button>
            <el-button v-show="validAccessAction(scope, 'sign')" size="mini" type="text" icon="el-icon-s-flag" v-hasPermi="['meet:appointment:sign']">
              签到
            </el-button>
            <el-button
              v-show="validAccessAction(scope, 'callback')"
              size="mini"
              type="text"
              icon="el-icon-s-opportunity"
              v-hasPermi="['meet:appointment:callback']"
            >
              回访
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['meet:appointment:remove']">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </table-list>
    <!-- 查看 -->
    <el-dialog title="查看约会管理" top="3vh" :visible.sync="view" width="600px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
      <edit-form :inline="true" ref="viewFormContainer" :editFormMap="viewFormMap" :editFormData.sync="viewForm" style="margin-right: 10px" />
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="view = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 后台人员确认地点和时间 -->
    <el-dialog title="确认约会管理" top="3vh" :visible.sync="confirm" width="600px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
      <edit-form
        :inline="true"
        ref="editFormContainer"
        :editFormMap="confirmFormMap"
        :editFormData.sync="confirmForm"
        :editFormRule="confirmFormRule"
        style="margin-right: 10px"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="confirm = false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { delAppointment, exportAppointment, getBackendUserList } from '@/api/meet/appointment';
import { getDataSource } from '@/static/js/dataSource';
import { assignData } from '@/utils';
import cloneDeep from 'lodash/cloneDeep';
import { mapState } from 'vuex';
export default {
  name: 'Appointment',
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
    }),
    filterMap() {
      return getDataSource('editForm', 'appointmentFilterMap');
    },
    tableMap() {
      return getDataSource('tableColumn', 'appointmentTableMap');
    },
    confirmFormMap() {
      return getDataSource('editForm', 'appointConfirmFormMap');
    },
    confirmFormRule() {
      return getDataSource('editRule', 'appointConfirmFormRule');
    },
    viewFormMap() {
      return getDataSource('editForm', 'appointViewFormMap');
    },
    tableDataList() {
      const mockData = require('@/static/mock/meet/appointment');
      const { tableList } = mockData;
      return tableList || [];
    },
  },
  watch: {
    view() {
      this.$nextTick(() => {
        const viewFormContainer = this.$refs.viewFormContainer;
        const viewFormData = viewFormContainer.$refs.editFormData;
        viewFormData.clearValidate();
      });
    },
    confirm() {
      this.$nextTick(() => {
        const editFormContainer = this.$refs.editFormContainer;
        const editFormData = editFormContainer.$refs.editFormData;
        editFormData.clearValidate();
      });
    },
  },
  data() {
    return {
      optionListMap: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表格数据
      userList: [],
      // 下拉数据对象
      tableOptionMap: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      confirm: false,
      view: false,
      // 查询参数
      queryParams: {},
      // 表单参数
      basicForm: {},
      // 确认表单
      confirmForm: {},
      // 查看表单
      viewForm: {},
      // 后台人员列表
      backendUserList: [],
    };
  },
  mounted() {
    this.getBackendUserList();
    this.getList();
  },
  methods: {
    getBackendUserList() {
      getBackendUserList().then((res) => {
        this.backendUserList = res.rows || [];
      });
    },
    /** 查询列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.tableListContainer.init();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      const tableListContainer = this.$refs.tableListContainer;
      tableListContainer.pagination = tableListContainer.$options.data().pagination;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      const filterContainer = this.$refs.filterContainer;
      filterContainer.resetForm('editFormData');
      this.handleQuery();
    },
    validAccessAction(scope, type) {
      const { appointmentStatus, followUpStatus, meetSignStatus, meetStatus } = scope.row;
      // 确认按钮显示条件
      let flag = false;
      if (type === 'confirm') {
        flag = appointmentStatus === 4 && !followUpStatus && !meetSignStatus && meetStatus === 1;
      } else if (type === 'sign') {
        flag = appointmentStatus === 4 && !followUpStatus && meetSignStatus === 2 && meetStatus === 1;
      } else if (type === 'callback') {
        flag = appointmentStatus === 5 && followUpStatus === 1 && meetSignStatus === 3 && meetStatus === 2;
      }
      return flag;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleView(row) {
      const propList = this.viewFormMap.map((item) => item.prop);
      this.viewForm = cloneDeep({ ...assignData(row, propList), id: row.id });
      this.view = true;
    },
    /** 确认按钮操作 */
    handleConfirm(row) {
      const propList = this.confirmFormMap.map((item) => item.prop);
      this.confirmForm = cloneDeep({ ...assignData(row, propList), id: row.id });
      this.confirm = true;
    },
    /** 新增按钮操作 */
    handleAdd() {},
    /** 修改按钮操作 */
    handleUpdate(row) {},
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row ? row.id : this.ids.join(',');
      this.$modal
        .confirm('是否确认删除Id编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAppointment(ids);
        })
        .then(() => {
          this.getList();
          this.$message.success('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const tableListContainer = this.$refs.tableListContainer;
      const pagination = tableListContainer.pagination;
      const params = {
        ...pagination,
        ...this.queryParams,
      };
      exportAppointment(params, '约会管理数据导出');
    },
    changeAction(prop) {},
  },
};
</script>
<style lang="scss" scoped></style>
